package com.example.springboot_template.config;

import cn.dev33.satoken.stp.StpInterface;
import com.example.springboot_template.Service.UserService;
import com.example.springboot_template.model.domain.User;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * 自定义权限加载接口实现类
 */
@Component
public class permissionConfig implements StpInterface {
    /**
     * 返回一个账号所拥有的权限码集合
     * @param loginId
     * @param loginType
     * @return
     */
    @Resource
    private UserService userService;
    @Override
    public List<String> getPermissionList(Object loginId, String loginType) {
        User user = userService.getById(Integer.parseInt((String) loginId));
        String permission = user.getPermission();
        // 去掉字符串两边的方括号
        permission = permission.replace("[", "").replace("]", "");

        // 将字符串拆分为数组
        String[] actionsArray = permission.split(",");

        // 将数组转换为List
        List<String> actionsList = Arrays.asList(actionsArray);
        return actionsList;
    }

    /**
     * 返回一个账号所拥有的角色标识集合
     * @param loginId
     * @param loginType
     * @return
     */
    @Override
    public List<String> getRoleList(Object loginId, String loginType) {
        return null;
    }
}
